package cn.systemManage.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.basic.GlobalConstants;
import cn.basic.action.BasicAction;
import cn.systemManage.bean.UserBean;
import cn.systemManage.dao.UserDao;
import cn.systemManage.service.UserService;
import cn.util.DateHepler;
import cn.util.MD5;
import cn.util.PageBean;
import cn.util.StringHelper;
@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Resource private UserDao userDao;
	/**
	 * 用户登录
	 * 
	 * @param userBean
	 * @return
	 * @throws Exception
	 */
	public String login(UserBean userBean) throws Exception{
		String result = "";
		if(userBean!=null){
			if(!StringHelper.isEmpty(userBean.getUsername())){
				String hql = "from UserBean where username=:_username";
				Map<Serializable, Serializable> condition = new HashMap<Serializable, Serializable>();
				condition.put("_username", userBean.getUsername());
				UserBean tmpUserBean = (UserBean) userDao.findObjByCondition(hql, condition);
				if(tmpUserBean!=null){
					if(MD5.getMD5(userBean.getPassword()).equals(tmpUserBean.getPassword())){
						HttpServletRequest request = ServletActionContext.getRequest();
						// 登录成功
						String ip = "";
						ip = request.getHeader("x-forwarded-for");
						if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
							ip = request.getHeader("Proxy-Client-IP");
						}
						if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
							ip = request.getHeader("WL-Proxy-Client-IP");
						}
						if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
							ip = request.getRemoteAddr();
						}
						if (ip.equals("0:0:0:0:0:0:0:1")) {
							ip = "127.0.0.1";
						}
						tmpUserBean.setLastLoginIp(ip);
						tmpUserBean.setLastLoginTime(DateHepler.getTimestampNowDate());
						this.userDao.updateObj(tmpUserBean);
						BasicAction.putSessinValue(GlobalConstants.CURRENT_USER, tmpUserBean);
						result = "1";
					}else{
						//密码错误
						result = "2";
					}
				}else{
					result = "4";
				}
			}else{
				result = "5";
			}
		}
		return result;
	}
	/**
	 * 取user分页数据
	 * @param userBean
	 * @return
	 * @throws Exception
	 */
	public PageBean getPageBean(UserBean userBean)throws Exception{
		Map<Serializable, Serializable> condition = new HashMap<Serializable, Serializable>();
		StringBuffer hql = new StringBuffer("from UserBean where 1=1");
		Integer pageNow = 0;
		Integer pageSize = 0;
		if(userBean!=null){
			if(userBean.getRealName()!=null&&userBean.getRealName().trim().length()>0){
				hql.append(" and realName=:_realName");
				condition.put("_realName", userBean.getRealName().trim());
			}
			if(userBean.getUsername()!=null&&userBean.getUsername().trim().length()>0){
				hql.append(" and username=:_username");
				condition.put("_username", userBean.getUsername().trim());
			}
			pageNow = userBean.getPageNow();
			if(pageNow==null||pageNow<1){
				pageNow = 1;
				userBean.setPageNow(pageNow);
			}
			pageSize = userBean.getPageSize();
			if(pageSize==null||pageSize<1){
				pageSize = PageBean.default_pageSiz;
				userBean.setPageSize(pageSize);
			}
		}
		return this.userDao.fetchPage(hql.toString(), condition, pageNow, pageSize);
	}
	/**
	 * 用于修改  根据id 取信息
	 * @param userBean
	 * @throws Exception
	 *
	 * @author  zhanghongliang
	 * @Date    2011-4-15下午01:55:52
	 */
	public UserBean findUserBeanById(UserBean userBean)throws Exception{
		if(userBean!=null){
			if(userBean.getId()>0){   //是修改   
				userBean = (UserBean) userDao.findObjById(UserBean.class, userBean.getId());
			}else {
				userBean.setSex(1);
			}
		}
		return userBean;
	}
	/**
	 * 保存用户
	 * @param userBean
	 * @return              1  保存成功；2 用户名已经存在；3 用户名为空；0保存失败
	 * @throws Exception
	 *
	 * @author  zhanghongliang
	 * @Date    2011-4-15下午01:59:31
	 */
	public int saveUserBean(UserBean userBean)throws Exception{
		int result =3;
		if(userBean!=null){
			if(userBean.getUsername()!=null){
				int count = this.userDao.checkIsExsit(userBean.getUsername(), userBean.getId());
				if(count>0){
					result =2;
				}else{
					int userId = userBean.getId();
					if(userId>0){
						UserBean tmpUserBean = (UserBean) this.userDao.findObjById(UserBean.class, userId);
						tmpUserBean.setUsername(userBean.getUsername());
						result = this.userDao.updateObj(tmpUserBean);
					}else{
						userBean.setIsAdmin(0);
						result = this.userDao.saveObj(userBean);
					}
				}
			}
		}
		return result;
	}
	/**
	 * 删除用户
	 * @param id 
	 * @return    1   成功；2 失败
	 * @throws Exception
	 *
	 * @author  zhanghongliang
	 * @Date    2011-4-15下午02:44:09
	 */
	public int deleteUserBean(Integer id)throws Exception{
		return this.userDao.deleteUserBeanById(id);
	}
}
